#!/usr/bin/env Rscript

rm(list = ls())
options(stringsAsFactors=FALSE)

suppressMessages(library(dplyr))

setwd("")

cat("\nReading..\n")
## voter record not provided
load(file="formattedByDyad_CaliAllYears_dobsPartiesHidsAlters_formattedMovers_withOccsGender.RData")
## this file was created to reduce memory usage

votes04a <- grep("cali07.1", names(DYAD))
names(DYAD)[votes04a] <- gsub(".1", "_a", names(DYAD)[votes04a])

cat("Formatting..\n")

DYAD <- subset(DYAD, !is.na(yob07) | !is.na(yob09)  | !is.na(yob10) | !is.na(yob12) | !is.na(yob14))

DYAD$party_cali12 <- sub("npp", "ds", DYAD$party_cali12)
DYAD$party_cali12_a<- sub("npp", "ds", DYAD$party_cali12_a)

DYAD$party_cali14 <- sub("npp", "ds", DYAD$party_cali14)
DYAD$party_cali14_a<- sub("npp", "ds", DYAD$party_cali14_a)

DYAD$age07 <- scale(
    log(2006 - DYAD$yob07),
    center=median(log(2006 - DYAD$yob07),na.rm=T)
)
DYAD$age09 <- scale(
    log(2008 - DYAD$yob09),
    center=median(log(2008 - DYAD$yob09),na.rm=T)
)
DYAD$age10 <- scale(
    log(2010 - DYAD$yob10),
    center=median(log(2010 - DYAD$yob10),na.rm=T)
)
DYAD$age12 <- scale(
    log(2012 - DYAD$yob12),
    center=median(log(2012 - DYAD$yob12),na.rm=T)
)

partyChangeSummaryPlacebo <- function(
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14")),
    lastvote_a=list(
        "09"="pg08_cali09_a",
        "10"="gg10_cali10_a",
        "12"="pg12_cali12_a",
        "14"="gg14_cali14_a"
    ),
    separating=TRUE,
    change=TRUE,
    partyT1="",
    partyT2="",
    votingAlter=FALSE,
    movingAlter=FALSE,
    concordance=TRUE,
    time=FALSE,
    controlMoving=FALSE
) {
    lm.strings <- lapply(x, function(x) {
        paste(
            ifelse(
                concordance,
                paste(
                    "glm(party_cali",x[2],
                    ifelse(separating,"!","="),
                    "=party_cali",x[2],"_a",
                    sep=""
                ),
                ""
            ),
            ifelse(
                concordance&change,
                "&",
                ""
                ),
            ifelse(
                change,
                paste(
                    "party_cali",x[1],"!=party_cali",x[2],
                    sep=""
                ),
                ""
                ),
            ifelse(
                partyT2!="",
                paste(
                    "& party_cali",x[2]," %in% c(",
                    paste(
                        "\"", partyT2[[1]], "\"",
                        sep="", collapse = ","
                    ),
                    ")",
                    sep=""
                ),
                ""
                ),
            " ~ ",
            "I(hid",
            ifelse(time,"14",x[2]),
            ifelse(separating,"!","="),"=hid",
            ifelse(time,"14",x[2]),"_a)",
            "*age",x[1]," + ",
            "I(hid",
            ifelse(time,"14",x[2]),
            ifelse(separating,"!","="),"=hid",
            ifelse(time,"14",x[2]),"_a)",
            ## "*totalvoting",x[1],".factor ",
            "",
            ifelse(
               partyT1=="",
                paste("+ party_cali",x[1],sep=""),
                ifelse(
                    partyT1[[1]][1]=="ds",
                    "",
                    paste("+ party_cali",x[1],sep="")
                    )
                ),
            " + I(party_cali",x[1],"!=party_cali",x[1],"_a) ",
            ifelse(
                controlMoving,
                paste("hid",x[1],"!=","hid",x[2],sep=""),
                ""
            ),
            "+ scale(abs(yobdiff",x[1],
            ")), data = subset(DYAD,  ",
            "!is.na(yob",x[1],") & !is.na(yob",x[2],")",
            "& hid",
            ifelse(time,"12",x[1]),
            ifelse(separating,"=","!"),"=hid",
            ifelse(time,"12",x[1]),"_a",
            ifelse(
                time,
                "& hid07!=\"\" & hid09!=\"\" & hid10!=\"\" & hid12!=\"\" & hid14!=\"\" & hid07_a!=\"\" & hid09_a!=\"\" & hid10_a!=\"\" & hid12_a!=\"\" & hid14_a!=\"\"",
                "& hid07!=\"\" & hid09!=\"\" & hid10!=\"\" & hid12!=\"\" & hid14!=\"\" & hid07_a!=\"\" & hid09_a!=\"\" & hid10_a!=\"\" & hid12_a!=\"\" & hid14_a!=\"\""
                ## ""
            ),
            ifelse(
                time,
                paste(
                    "& (hid07!=hid07_a & hid09!=hid09_a & hid10!=hid10_a & hid12!=hid12_a & hid14==hid14_a ",
                    ## "| hid07==hid07_a & hid09==hid09_a & hid10==hid10_a & hid14!=hid14_a ",
                    "| hid07==hid07_a & hid09==hid09_a & hid10==hid10_a & hid12==hid12_a & hid14!=hid14_a ",
                    "| hid07==hid07_a & hid09==hid09_a & hid10==hid10_a & hid12==hid12_a & hid14==hid14_a)",
                    sep=""
                    ),
                ""
            ),
            "& hid",x[1],"!=\"\" & hid",x[2],"!=\"\"",
            "& hid",x[1],"_a!=\"\" & hid",x[2],"_a!=\"\"",
            "& abs(yobdiff",x[1],") <= 15",
            "& hid",ifelse(time,"12",x[1]),"!=hid",ifelse(time,"14",x[2])," ",
            ifelse(
                partyT1!="",
                paste(
                    "& party_cali",x[1]," %in% c(",
                    paste(
                        "\"", partyT1[[1]], "\"",
                        sep="", collapse = ","
                    ),
                    ") & party_cali",x[1],"_a %in% c(",
                    paste(
                        "\"", partyT1[[2]], "\"",
                        sep="", collapse = ","
                    ),
                    ")",
                    sep=""
                ),
                ""
                ),
            ifelse(
                movingAlter,
                paste(
                    " & ","(hid",x[1],"_a!=hid",x[2],"_a | ",
                    lastvote_a[[x[2]]],"==1)",
                    sep=""),
                ""
            ),
            ifelse(
                votingAlter,
                paste(" & ",lastvote_a[[x[2]]],"==1",sep=""),
                ""
            ),
            "), family=binomial)",
            sep = ""
        )
    }
                         )
    lapply(lm.strings, print)
    the.mods <- lapply(lm.strings, function(y) eval(parse(text = y)))
    paste(lapply(the.mods, nobs), collapse = " & ")
    return(the.mods)
}


########
########

####
####
cat("\nModeling (2a)..\n")
i <- 0
pb <- txtProgressBar(style=3)
DYAD.partyChangeModels.time2.glm.votes.placebo <- partyChangeSummaryPlacebo(
    time=TRUE,
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14"))
)
i <- i+1/7; setTxtProgressBar(pb, i)
DYAD.partyChangeModels.Switch.time2.glm.votes.placebo <- partyChangeSummaryPlacebo(
    time=TRUE,
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14")),
    partyT1=list(c("dem","rep"),c("dem","rep")),
    partyT2=list(c("dem","rep"),c("dem","rep"))
)
i <- i+1/7; setTxtProgressBar(pb, i)
DYAD.partyChangeModels.Affil.time2.glm.votes.placebo <- partyChangeSummaryPlacebo(
    time=TRUE,
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14")),
    partyT1=list(c("dem","rep"),c("dem","rep"))
)
i <- i+1/7; setTxtProgressBar(pb, i)
DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo <- partyChangeSummaryPlacebo(
    time=TRUE,
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14")),
    partyT1=list(c("dem","rep"),c("dem","rep")),
    partyT2=list(c("ds"))
)
i <- i+1/7; setTxtProgressBar(pb, i)
DYAD.partyChangeModels.noAffil.time2.glm.votes.placebo <- partyChangeSummaryPlacebo(
    time=TRUE,
    x=list(c("07","09"), c("09","10"),c("10","12"),c("12","14")),
    partyT1=list(c("ds"),c("ds"))
)
i <- i+1/7; setTxtProgressBar(pb, i)
close(pb)



DYAD.partyChangeModels.time2.glm.votes.placebo.coefs <- lapply(DYAD.partyChangeModels.time2.glm.votes.placebo, coef)
DYAD.partyChangeModels.time2.glm.votes.placebo.ses <- lapply(DYAD.partyChangeModels.time2.glm.votes.placebo, function(x) summary(x)$coefficients[,2])
DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.coefs <- lapply(DYAD.partyChangeModels.Switch.time2.glm.votes.placebo, coef)
DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.ses <- lapply(DYAD.partyChangeModels.Switch.time2.glm.votes.placebo, function(x) summary(x)$coefficients[,2])
DYAD.partyChangeModels.Affil.time2.glm.votes.placebo.coefs <- lapply(DYAD.partyChangeModels.Affil.time2.glm.votes.placebo, coef)
DYAD.partyChangeModels.Affil.time2.glm.votes.placebo.ses <- lapply(DYAD.partyChangeModels.Affil.time2.glm.votes.placebo, function(x) summary(x)$coefficients[,2])
DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.coefs <- lapply(DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo, coef)
DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.ses <- lapply(DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo, function(x) summary(x)$coefficients[,2])

save(
    DYAD.partyChangeModels.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.time2.glm.votes.placebo.ses,
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.Switch.time2.glm.votes.placebo.ses,
    DYAD.partyChangeModels.Affil.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.Affil.time2.glm.votes.placebo.ses,
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.DropAffil.time2.glm.votes.placebo.ses,
    DYAD.partyChangeModels.noAffil.time2.glm.votes.placebo.coefs,
    DYAD.partyChangeModels.noAffil.time2.glm.votes.placebo.ses,
    file="party_change_models_time2_glm_votes_together_placebo_coefsonly.RData"
    )
